<?php 
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use app\models\Comment;
class Article extends ActiveRecord
{
    /**
     * @return string 返回该AR类关联的数据表名
     */
    public static function tableName()
    {
        return 'article';
    }
       public function rules()
        {
            return [
                [['article_title', 'article_content'], 'required']];
        }
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'article_title' => '开头',
            'article_content' => '内容',
        ];
    }
    // 根据title来获取整个文章的数据
    public static function getArticle($title)
    {
       
        $article = Article::find()
         ->where(['article_title' => $title])
         ->one();
         return $article;
    }
    // 获得前几篇新编辑的文章，$num为获得的数量
    public static function getNewArticle($num)
    {
          return Article::find()->limit($num)->orderBy('id desc')->all();

    }
    /* 
    和index的翻页有关，pageSize为页面最多存在的文章数，orderBy('id desc')为文章的排序。需要返回两个变量 $pagination和$articles
    */
    public static function getPaginationArt($pagesize)
    {
        $query = Article::find();
        $count = $query->count();
       $pagination = new Pagination(['totalCount' => $count,'pageSize' => $pagesize]);
       $articles = $query->offset($pagination->offset)
        ->limit($pagination->limit)
            ->orderBy('id desc')
        ->all();
        $paginationArt['pagination']= $pagination;
        $paginationArt['articles']= $articles;
        return $paginationArt;
    }
   
    public static function getArticleById($id)
    {
        $article = static::find()->where(['id'=>$id])->one();
        return $article;
    }
}

 ?>